<template>
  <div class="home_box">
    <el-container class="home-container">
      <el-header>
        <div>
          <!-- <img src="../../assets/left1.jpg" width="100" alt="" /> -->
          <span>基于web的个人图书共享系统设计与实现</span>
          <p class="time"></p>
          <div class="outbtn">
            <span class="tie">欢迎您！{{ admin }}</span>
            <el-button type="success" @click="logout">退出</el-button>
          </div>
        </div>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toogleButn">|||</div>
          <el-menu
            background-color="#1486a0"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <!-- 一级菜单 数值和字符串拼接 就是一个字符串 -->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <i :class="item.ico" id="ico_text"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主体内容 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
function getAllTime() {
  setInterval(function() {
    let p = document.querySelector('.time')
    let date = new Date()
    const y = date.getFullYear()
    const m = (date.getMonth() + 1 + '').padStart(2, '0')
    const d = (date.getDate() + '').padStart(2, '0')
    const hh = (date.getHours() + '').padStart(2, '0')
    const mm = (date.getMinutes() + '').padStart(2, '0')
    const ss = (date.getSeconds() + '').padStart(2, '0')
    p.innerHTML = y + '年' + m + '月' + d + '日 ' + hh + ':' + mm + ':' + ss
  }, 1000)
}
getAllTime()
export default {
  data() {
    return {
      admin: localStorage.getItem('adminname'),
      // 菜单列表
      menuList: [
        {
          id: 1,
          authName: '用户管理',
          path: 'userlist',
          ico: 'el-icon-s-custom',
          children: [{ id: 1, authName: '用户列表', path: 'userlist' }]
        },
        {
          id: 2,
          authName: '书籍管理',
          path: 'booklist',
          ico: 'el-icon-s-order',
          children: [
            { id: 1, authName: '非共享书籍', path: 'booklist' },
            { id: 2, authName: '共享书籍', path: 'sharebooklist' }
          ]
        },
        {
          id: 3,
          authName: '书籍审核管理',
          path: 'bookreview',
          ico: 'el-icon-s-check',
          children: [{ id: 1, authName: '审核列表', path: 'bookreview' }]
        },
        {
          id: 4,
          authName: '书籍分类管理',
          path: 'booklist',
          ico: 'el-icon-s-data',
          children: [{ id: 1, authName: '分类列表', path: 'bookcatelist' }]
        },
        {
          id: 5,
          authName: '公告管理',
          path: 'ouncelist',
          ico: 'el-icon-s-management',
          children: [
            { id: 1, authName: '公告列表', path: 'ouncelist' },
            { id: 2, authName: '发布公告', path: 'declareinfo' }
          ]
        },
        {
          id: 6,
          authName: '反馈留言管理',
          path: 'leavemanager',
          ico: 'el-icon-s-promotion',
          children: [{ id: 1, authName: '留言列表', path: 'leavemanager' }]
        }
      ],
      // 是否折叠
      isCollapse: false,
      // 被激活的地址
      activePath: '',
      currentTime: ''
    }
  },
  created() {
    // 当Home被创建的时候，路径被赋值
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出功能
    logout() {
      localStorage.removeItem('adminname')
      this.$router.push('/login')
    },
    // 控制是否折叠
    toogleButn() {
      this.isCollapse = !this.isCollapse
    },
    // 保存激活状态
    saveNavState(active) {
      this.activePath = active
      window.sessionStorage.setItem('activePath', active)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container,
.home_box {
  height: 100%;
}
.el-header {
  background-color: #2e95ce;
  display: flex;
  width: 100%;
  > div > span {
    vertical-align: middle;
    font-size: 30px;
    letter-spacing: 5px;
    font-family: '隶书';
    color: #fbf7f9;
    text-shadow: 0px 3px 7px #ffffff;
    display: inline-block;
    padding: 13px 0;
  }
  > div > img {
    vertical-align: middle;
  }
  > div > .outbtn {
    position: absolute;
    right: 20px;
    top: 10px;
    .tie {
      font-size: 26px;
      font-family: '隶书';
      color: #fbf7f9;
      display: inline-block;
      padding-right: 10px;
      vertical-align: middle;
    }
  }
}
.el-aside {
  background-color: #1486a0;
}
.el-main {
  background-color: #2aa9dd;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: 0px;
}
.toggle-button {
  background-color: rgb(23, 178, 240);
  color: white;
  font-size: 18px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
.time {
  display: inline-block;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 20px;
  font-size: 18px;
  color: white;
  left: 45%;
}
.el-submenu__title i {
  color: #fff !important;
}
#ico_text {
  color: #fff !important;
}
</style>
